//
//  GDHFacePageView.m
//  GDHChatKeyBoard
//
//  Created by 高得华 on 2017/10/12.
//  Copyright © 2017年 高得华. All rights reserved.
//

#import "GDHFacePageView.h"
#import "GDHFaceThemeView.h"
#import "GDHFaceButton.h"
#import "GDHChatKeyBoardHeader.h"

#define FaceContainerHeight       kFacePanelHeight() - kFacePanelBottomToolBarHeight - kUIPageControllerHeight //146

static NSString * const DeleteKey = @"DeleteKey";
//#define DeleteKey       @"DeleteKey"

NSString * const GDHFacePageViewFaceSelectedNotification = @"GDHFacePageViewFaceSelectedNotification";
NSString * const GDHFacePageViewFaceName = @"GDHFacePageViewFaceName";
NSString * const GDHFacePageViewDeleteKey = @"GDHFacePageViewDeleteKey";
NSString * const GDHFacePageViewFaceThemeStyle = @"GDHFacePageViewFaceThemeStyle";

@interface GDHFacePageView ()

/** 按钮数组 */
@property (nonatomic, strong) NSMutableArray * buttons;

@end

@implementation GDHFacePageView

- (void)prepareForReuse
{
    [super prepareForReuse];
    DTLog(@"prepareForReuse------------------------------------------");
    DTLog(@"%zd", self.themeStyle);
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor whiteColor];
        [self initSystemEmojiFace];
    }
    return self;
}

- (void)setThemeStyle:(GDHFaceThemeStyle)themeStyle
{
    if (_themeStyle != themeStyle)
    {
        _themeStyle = themeStyle;
        
        switch (_themeStyle) {
            case GDHFaceThemeStyleSystemEmoji:
                [self initSystemEmojiFace];
                break;
            case GDHFaceThemeStyleCustomEmoji:
                [self initCustomEmojiFace];
                break;
            case GDHFaceThemeStyleGif:
                [self initGifFace];
                break;
            default:
                break;
        }
    }
}

- (void)initSystemEmojiFace
{
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSInteger cols = 7;
    CGFloat width = [[UIScreen mainScreen] bounds].size.width;
    if (width == 320)
        cols = 7;
    else if (width == 375)
        cols = 8;
    else if (width == 414)
        cols = 9;
    NSInteger lines = 3;
    
    CGFloat item = 40.f;//30
    NSInteger edgeDistance = 10.f;
    
    CGFloat vMargin = (FaceContainerHeight - lines * item) / (lines+1);
    CGFloat hMargin = (CGRectGetWidth(self.bounds) - cols * item - 2*edgeDistance) / cols;
    
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < lines; ++i) {
        for (int j = 0; j < cols; ++j) {
            GDHFaceButton *btn = [GDHFaceButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(j*item+edgeDistance+j*hMargin,i*item+(i+1)*vMargin,item,item);
            [btn addTarget:self action:@selector(faceBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:btn];
            [array addObject:btn];
        }
    }
    self.buttons = array;
}

- (void)initCustomEmojiFace
{
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSInteger cols = 7;
    CGFloat width = [[UIScreen mainScreen] bounds].size.width;
    if (width == 320)
        cols = 7;
    else if (width == 375)
        cols = 8;
    else if (width == 414)
        cols = 9;
    
    NSInteger lines = 3;
    CGFloat item = 40.f;
    NSInteger edgeDistance = 10;
    
    CGFloat vMargin = (FaceContainerHeight - lines * item) / (lines+1);
    
    CGFloat hMargin = (CGRectGetWidth(self.bounds) - cols * item - 2*edgeDistance) / cols;
    
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < lines; ++i) {
        for (int j = 0; j < cols; ++j) {
            GDHFaceButton *btn = [GDHFaceButton buttonWithType:UIButtonTypeCustom];
            [btn addTarget:self action:@selector(faceBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [array addObject:btn];
            [self.contentView addSubview:btn];
            btn.frame = CGRectMake(j*item+edgeDistance+j*hMargin,i*item+(i+1)*vMargin,item,item);
        }
    }
    self.buttons = array;
}

- (void)initGifFace
{
    [self.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSInteger cols = 4;
    NSInteger lines = 2;
    CGFloat item = 60.f;
    
    CGFloat vMargin = (FaceContainerHeight - lines * item) / (lines+1);
    CGFloat hMargin = (CGRectGetWidth(self.bounds) - cols * item) / (cols+1);
    
    NSMutableArray *array = [NSMutableArray array];
    
    for (int i = 0; i < lines; ++i) {
        for (int j = 0; j < cols; ++j) {
            GDHFaceButton *btn = [GDHFaceButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(j*item+(j+1)*hMargin,i*item+(i+1)*vMargin,item,item);
            [btn addTarget:self action:@selector(faceBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.contentView addSubview:btn];
            [array addObject:btn];
        }
    }
    self.buttons = array;
}

- (void)loadPerPageFaceData:(NSArray *)faceData;
{
    switch (_themeStyle) {
        case GDHFaceThemeStyleSystemEmoji:
            [self loadSystemEmoji:faceData];
            break;
        case GDHFaceThemeStyleCustomEmoji:
            [self loadCustomEmoji:faceData];
            break;
        case GDHFaceThemeStyleGif:
            [self loadGifEmoji:faceData];
            break;
        default:
            break;
    }
}

- (void)loadSystemEmoji:(NSArray *)faceData
{
    for (NSInteger i = 0; i < self.buttons.count; ++i) {
        GDHFaceButton *btn = self.buttons[i];
        if (i < faceData.count) {
            GDHFaceModel *fm = faceData[i];
            btn.hidden = NO;
            [btn setTitle:fm.faceIcon forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:25.f];
            [btn setImage:nil forState:UIControlStateNormal];
            btn.faceTitle = fm.faceTitle;
        }else if (i == faceData.count) {
            btn.hidden = NO;
            [btn setTitle:nil forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:buttonDelete_ios7] forState:UIControlStateNormal];
            btn.faceTitle = DeleteKey;
        }else{
            btn.hidden = YES;
            btn.faceTitle = nil;
            [btn setImage:nil forState:UIControlStateNormal];
            [btn setTitle:nil forState:UIControlStateNormal];
        }
    }
}

- (void)loadCustomEmoji:(NSArray *)faceData
{
    for (NSInteger i = 0; i < self.buttons.count; ++i) {
        GDHFaceButton *btn = self.buttons[i];
        if (i < faceData.count) {
            GDHFaceModel *fm = faceData[i];
            btn.hidden = NO;//[UIImage imageNamed:fm.faceIcon]
            [btn setImage:[UIImage imageWithContentsOfFile:fm.faceIcon] forState:UIControlStateNormal];
            btn.faceTitle = fm.faceTitle;
        }else if (i == faceData.count) {
            btn.hidden = NO;
            [btn setImage:[UIImage imageNamed:buttonDelete_ios7] forState:UIControlStateNormal];
            btn.faceTitle = DeleteKey;
        }else{
            btn.hidden = YES;
            btn.faceTitle = nil;
            [btn setImage:nil forState:UIControlStateNormal];
        }
    }
}

- (void)loadGifEmoji:(NSArray *)faceData
{
    for (NSInteger i = 0; i < self.buttons.count; ++i) {
        GDHFaceButton *btn = self.buttons[i];
        if (i < faceData.count) {
            GDHFaceModel *fm = faceData[i];
            btn.hidden = NO;//[UIImage imageNamed:fm.faceIcon]
            [btn setImage:[UIImage imageWithContentsOfFile:fm.faceIcon] forState:UIControlStateNormal];
            btn.faceTitle = fm.faceTitle;
        }else{
            btn.hidden = YES;
            btn.faceTitle = nil;
            [btn setImage:nil forState:UIControlStateNormal];
        }
    }
}

- (void)faceBtnClick:(GDHFaceButton *)button
{
    BOOL deleteKey = NO;
    if ([button.faceTitle isEqualToString:DeleteKey]) {
        deleteKey = YES;
    }
    
    NSDictionary *faceInfo = @{
                               GDHFacePageViewFaceName : button.faceTitle ? button.faceTitle : @"",
                               GDHFacePageViewDeleteKey : @(deleteKey),
                               GDHFacePageViewFaceThemeStyle : @(self.themeStyle)
                               };
    
    NSNotification *notification = [NSNotification notificationWithName:GDHFacePageViewFaceSelectedNotification object:self];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:GDHFacePageViewFaceSelectedNotification object:notification userInfo:faceInfo];
}

@end

